<?php
// +----------------------------------------------------------------------
// | 奇乐中介担保交易系统 免费版 2019
// +----------------------------------------------------------------------
// | 官方网址：http://www.qilecms.com   
// +----------------------------------------------------------------------
// | 产品文档：http://www.qilecms.com/doc
// +----------------------------------------------------------------------
// | 开发作者: 奇乐CMS
// +----------------------------------------------------------------------
// | 版权说明：这不是一个自由软件！本产品仅限个人使用，但请务必保留底部版权，以免产生不必要的纠纷,
// | 您只能在不用于商业目的的前提下对程序代码进行修改和使用，
// | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\DB;
use think\facade\Request;
class Info extends Category
{

	public function set(){
		return $this->fetch();
	}

	public function index(){
      $alias = 'a';        //需要别名时写别名，不需要的时候留空 
      $defaultWhere = '';  //默认where
      $defaultOrder = 'a.create_time desc';  //默认排序
    //接收搜索表单数据
      $SearchFormData = $this->getSearchFormData($defaultWhere,$defaultOrder);
      $where = $SearchFormData['where'];
      $order = $SearchFormData['order'];
      $pageNum = $SearchFormData['pageNum'];

//组建关键词数据
     $keywordsData  =[
                  [
                  'field'   =>'id', //字段名
                  'name'    =>'文章ID', //文本显示名称
                  'alias'   => $alias,  //字段对应的别名
                  ],
                  [
                  'field'   =>'title',
                  'name'    =>'文章标题',
                  'alias'   => $alias,
                  ]
     ];
//组建日期筛选   
       $dateData = [
                  [
                  'title'        =>'创建时间',  //标签显示名称
                  'field'        =>'create_time', //筛选字段
                  'start_name'   =>'开始时间', //开始时间
                  'end_name'     =>'结束时间', //结束时间
                  'alias'        => $alias,  //字段别名
                  ],

              ];
//单个字段筛选
    $customSingleField =[
                       ]; 
// 组件字段组筛选
     $groupData    = [];
     $orderData   = [];  

     $this->assign('keywordsData',$keywordsData);
     $this->assign('customSingleField',$customSingleField);
     $this->assign('groupData',$groupData);
     $this->assign('dateData',$dateData);
     $this->assign('orderData',$orderData);


    
     $lists = DB::name('info')->alias('a')
     ->field('a.id,a.title,a.create_time,b.cname,b.alias,b.pid')
     ->join('info_category b','a.cid = b.cid',"LEFT")
     ->where($where)
     ->order($order)
     ->paginate($pageNum);
     $page = $lists->render(); 
     $list =$lists->toArray()['data'];  

     $cate = $this->getCache('infoCategory','cid');

     foreach($list as $k=>$v){
      
       $list[$k]['parent_alias'] = $cate[$v['pid']]['alias'];

     } 

     $this->assign('page', $page);
     $this->assign('list',$list);
	  return $this->fetch();
	} 


	public function add(){
  $param =$this->request->param();

     if($this->request->isPost()){
    

           //表单验证
           $rule =[
              'title'=>'require|max:150',
              'content'=>'require',
            ];
            $msg= [
             'title.require' =>'站内文章标题不能为空',
             'title.max' =>'站内文章标题不能超过150个字',
             'content.require' =>'站内文章内容不能为空',
            
            ];
             $error = $this->checkSubmit($param,$rule,$msg);  
            if($error){
                return error_json($error);
             }
        //三级分类判断    
                      if(!empty($param['cate']['three'])){
                            $param['cid'] = $param['cate']['three'];
                      }else{
                         if(!empty($param['cate']['two'])){
                             $param['cid'] = $param['cate']['two'];
                         }else{
                            if(!empty($param['cate']['one'])){
                             $param['cid'] = $param['cate']['one']; 
                            }
                         }
                      }

            $data['create_time'] = time();
            $data['title'] = $param['title'];
            $data['content'] = $param['content'];
            $data['hits'] = $param['hits'];
            $data['cid'] = $param['cid'];
            $result =  model('info')->addData($data);
          
       if($result){
              return success_json('添加成功');
             
            }else{
               return error_json('添加失败'); 
            }
           
   
        }else{

           return $this->fetch();

        }

	}
	public function edit(){
      $param = $this->request->param();

     if($this->request->isPost()){
   
               //表单验证
               $rule =[
                  'title'=>'require|max:150',
                  'content'=>'require',
                ];
                $msg= [
                 'title.require' =>'站内文章标题不能为空',
                 'title.max' =>'站内文章标题不能超过150个字',
                 'content.require' =>'站内文章内容不能为空',
                
                ];
                 $error = $this->checkSubmit($param,$rule,$msg);  
                if($error){
                    return error_json($error);
                 }
                      if(!empty($param['cate']['three'])){
                            $param['cid'] = $param['cate']['three'];
                      }else{
                         if(!empty($param['cate']['two'])){
                             $param['cid'] = $param['cate']['two'];
                         }else{
                            if(!empty($param['cate']['one'])){
                             $param['cid'] = $param['cate']['one']; 
                            }
                         }
                      }
                $data['create_time'] = time();
                $data['update_time'] = time();
                $data['title'] =$param['title'];
                $data['hits'] = $param['hits'];
                $data['content'] = $param['content'];
                $data['cid'] = $param['cid'];

                $where['id'] = $param['id'];
                $result =  model('info')->editData($data,$where);
                if($result){
                    return success_json('更新成功');
                     
                  }else{
                     return error_json('更新失败');
                  }
                  

         }else{
             if(!$param['id']){  return error_json('请求参数错误');}
              $info = model('info')->getInfoById($param['id']);

              $this->assign('info',$info);
              return $this->fetch();
         }

	}
	public function del(){
     $param = input();
     if(!$param['id']){  return error_json('请求参数错误');    }
     $id =$param['id'];
      if(is_array($id)){
       $ids = implode(',',$id);
     }else{
       $ids = $id;
     }  
     $where[]= ['id','in',$ids];
     $res= model('info')->delData($where);
     if($res){
      return success_json('删除成功');
     }else{
      return error_json($res);
     }

	}
  
   public  function category(){

      return parent::category();
   }

   public  function categoryAdd(){

      return parent::categoryAdd();
   }
   public  function categoryEdit(){

      return parent::categoryEdit();
   }
   public  function categoryUpdate(){

      return parent::updateCategoryCache();
   }
   public  function categoryDel(){

      return parent::del();
   }




}